package com.rk.hosp.controller.api;

import com.rk.common.result.Result;
import com.rk.hosp.model.hosp.Hospital;
import com.rk.hosp.service.DepartmentService;
import com.rk.hosp.service.HospitalService;
import com.rk.hosp.service.HospitalSetService;
import com.rk.hosp.service.ScheduleService;
import com.rk.hosp.vo.hosp.HospitalQueryVo;
import com.rk.hosp.vo.hosp.ScheduleOrderVo;
import com.rk.hosp.vo.order.SignInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author :Rk.
 * @date : 2022/11/19
 */
@Slf4j
@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp/hsopital")
public class HospApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;


    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    @ApiOperation(value = "获取分页列表")
//    @Cacheable(value = "hospital",keyGenerator = "keyGenerator")
    @GetMapping("/findHospList/{page}/{limit}")
    public Result index(
            @PathVariable Integer page,
            @PathVariable Integer limit,
            HospitalQueryVo hospitalQueryVo) {
        try {
            Page<Hospital> hospitals = hospitalService.selectPage(page, limit, hospitalQueryVo);
            return Result.ok(hospitalService.selectPage(page, limit, hospitalQueryVo));
        }catch (Exception e){
            return Result.fail("查询失败!");
        }
    }

    @ApiOperation(value = "通过医院名称查询医院信息")
    @GetMapping("/findHospByName/{hospName}")
    public Result findHospByName(@PathVariable String hospName){
        List<Hospital> list = hospitalService.findHospByName(hospName);
        return Result.ok(list);
    }

    @ApiOperation(value = "获取科室列表")
    @GetMapping("/department/{hoscode}")
    public Result index(
            @ApiParam(name = "hoscode", value = "医院code", required = true)
            @PathVariable String hoscode) {
        return Result.ok(departmentService.findDeptTree(hoscode));
    }


    @ApiOperation(value = "获取医院详情信息")
    @GetMapping("/findHospByhosCode/{hoscode}")
    public Result findHospByhosCode(@PathVariable String hoscode){
        Map<String, Object> item = hospitalService.item(hoscode);
        return Result.ok(item);
    }



    @ApiOperation(value = "获取可预约排班数据")
    @GetMapping("/auth/getBookingScheduleRule/{page}/{limit}/{hoscode}/{depcode}")
    public Result getBookingSchedule(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Integer page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Integer limit,
            @ApiParam(name = "hoscode", value = "医院code", required = true)
            @PathVariable String hoscode,
            @ApiParam(name = "depcode", value = "科室code", required = true)
            @PathVariable String depcode) {
        return Result.ok(scheduleService.getBookingScheduleRule(page, limit, hoscode, depcode));
    }


    @ApiOperation(value = "获取排班数据")
    @GetMapping("/auth/findScheduleList/{hoscode}/{depcode}/{workDate}")
    public Result findScheduleList(
            @ApiParam(name = "hoscode", value = "医院code", required = true)
            @PathVariable String hoscode,
            @ApiParam(name = "depcode", value = "科室code", required = true)
            @PathVariable String depcode,
            @ApiParam(name = "workDate", value = "排班日期", required = true)
            @PathVariable String workDate) {
        return Result.ok(scheduleService.getDetailSchedule(hoscode, depcode, workDate));
    }


    @ApiOperation(value = "根据排班id获取排班数据")
    @GetMapping("/getSchedule/{scheduleId}")
    public Result getSchedule(
            @ApiParam(name = "scheduleId", value = "排班id", required = true)
            @PathVariable String scheduleId) {
        return Result.ok(scheduleService.getById(scheduleId));
    }

    @ApiOperation(value = "根据排班id获取预约下单数据")
    @GetMapping("/inner/getScheduleOrderVo/{scheduleId}")
    public ScheduleOrderVo getScheduleOrderVo(
            @PathVariable("scheduleId") String scheduleId) {
        return scheduleService.getScheduleOrderVo(scheduleId);
    }


    @ApiOperation(value = "获取医院签名信息")
    @GetMapping("inner/getSignInfoVo/{hoscode}")
    public SignInfoVo getSignInfoVo(
            @ApiParam(name = "hoscode", value = "医院code", required = true)
            @PathVariable("hoscode") String hoscode) {
        return hospitalSetService.getSignInfoVo(hoscode);
    }
}
